package com.bawei.mall.product.service.impl;

import com.bawei.mall.product.domain.CuusBrand;
import com.bawei.mall.product.domain.MallProductHome;
import com.bawei.mall.product.domain.MallProductInfo;
import com.bawei.mall.product.mapper.MallProductAdvertiseMapper;
import com.bawei.mall.product.mapper.MallProductBrandInfoMapper;
import com.bawei.mall.product.mapper.MallProductInfoMapper;
import com.bawei.mall.product.service.MallProductHomeService;
import com.bawei.system.domain.MallProductAdvertise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.product.service.impl
 * @Project：mall_cloud
 * @name：MallProductHomeServiceImpl
 * @Date：2024/7/1 20:13
 */
@Service
public class MallProductHomeServiceImpl implements MallProductHomeService {

    @Autowired
    private MallProductAdvertiseMapper advertiseMapper;
    @Autowired
    private MallProductBrandInfoMapper brandInfoMapper;
    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;
    @Override
    public MallProductHome content() {
        MallProductHome mallProductHomes =new MallProductHome();
        CompletableFuture<List<MallProductAdvertise>> cf1 = CompletableFuture.supplyAsync(() -> {
            return advertiseMapper.list();
        }).exceptionally(ex -> {
            // Handle exception appropriately
            return Collections.emptyList(); // or throw a more specific exception
        });

        CompletableFuture<List<CuusBrand>> cf2 = CompletableFuture.supplyAsync(() -> {
            return brandInfoMapper.list2();
        }).exceptionally(ex -> {
            // Handle exception appropriately
            return Collections.emptyList(); // or throw a more specific exception
        });

        CompletableFuture<List<MallProductInfo>> cf3 = CompletableFuture.supplyAsync(() -> {
            return mallProductInfoMapper.selectList();
        }).exceptionally(ex -> {
            // Handle exception appropriately
            return Collections.emptyList(); // or throw a more specific exception
        });

        CompletableFuture<List<MallProductInfo>> cf4 = CompletableFuture.supplyAsync(() -> {
            return mallProductInfoMapper.selectListCommend();
        }).exceptionally(ex -> {
            // Handle exception appropriately
            return Collections.emptyList(); // or throw a more specific exception
        });

        CompletableFuture.allOf(cf1, cf2, cf3, cf4).join();

        try {
            List<MallProductAdvertise> list1 = cf1.get();
            List<CuusBrand> list2 = cf2.get();
            List<MallProductInfo> list3 = cf3.get();
            List<MallProductInfo> list4 = cf4.get();


            mallProductHomes.setAdvertiseList(list1);
            mallProductHomes.setBrandList(list2);
            mallProductHomes.setNewProductList(list3);
            mallProductHomes.setHotProductList(list4);
        } catch (Exception e) {
            // Handle InterruptedException and ExecutionException appropriately
            throw new RuntimeException(e);
        }
        return mallProductHomes;
    }




}
